local http_client = {}

local skywalking_tracer = require("skywalking.tracer")
local ngx = ngx

http_client.location_capture = function(url, option)
    local injector = function(header, value)
        option.header = option.header or {}
        option.header[header] = value
    end
    local http_client_exit_span = skywalking_tracer:createExitSpan(ngx.ctx, url, url, injector)
    http_client_exit_span:setComponentId(http_client_exit_span.ComponentId_Constants.NginxComponentId)
    http_client_exit_span:setLayer(http_client_exit_span.SpanLayer.HTTP)

    local res = ngx.location.capture(url, option)
    -- 打一些必要的标签
    http_client_exit_span:tag(http_client_exit_span.Tag_Constants.TagHTTPStatus, res.status)
    http_client_exit_span:finish(ngx.now() * 1000)
    if res.status ~= 200 then
        http_client_exit_span:error(ngx.now() * 1000, { ["res.body:"] = tostring(res.body)})
    end

    return res
end

return http_client
